


/**
 * 过滤空参数
 * @return {string} ojgdvbvaua40
 */
export function filterParams(params) {
  for (const i in params) {
    if (params[i] === null || params[i] === undefined || params[i] === '' || params[i].length == 0) {
      delete params[i];
    }
  }
  return params;
}

// 退出全屏
export function NoFullScreen(){
  // 防止报错
  // Uncaught (in promise) TypeError: Failed to execute 'exitFullscreen' on 'Document': Document not active
  try{
    if (document.exitFullscreen) {
      document.exitFullscreen();
    }else if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  }catch{
    if (document.webkitCancelFullScreen) {
      document.webkitCancelFullScreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    }
  }
} 
// 打开全屏
export function OpenFullScreen(){
  let element = document.documentElement;
  if (element.requestFullscreen) {
    element.requestFullscreen();
  } else if (element.webkitRequestFullScreen) {
    element.webkitRequestFullScreen();
  } else if (element.mozRequestFullScreen) {
    element.mozRequestFullScreen();
  } else if (element.msRequestFullscreen) {
    // IE11
    element.msRequestFullscreen();
  }
} 



// 绘制验证码
/*
drawCode("verifyCanvas").then(res=>{
  res.code
})
*/ 
export function drawCode(canvasId) {
  return new Promise((resolv,rejct)=>{
    try{
    const nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0", 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
      'L', 'M', 'N', 'O', 'P', 'Q', 'R',
      'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
      'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x',
      'y', 'z'
    ];
    var canvas = document.getElementById(canvasId); //获取HTML端画布
    var context = canvas.getContext("2d"); //获取画布2D上下文
    context.clearRect(0, 0, canvas.width, canvas.height);//清除canvas
    context.beginPath();//重新开始绘画
    context.fillStyle = "cornflowerblue"; //画布填充色
    context.fillRect(0, 0, canvas.width, canvas.height);
    // 创建渐变
    var gradient = context.createLinearGradient(0, 0, canvas.width, 0);
    gradient.addColorStop("0", "magenta");
    gradient.addColorStop("0.5", "blue");
    gradient.addColorStop("1.0", "red");
    //清空画布
    context.fillStyle = gradient; //设置字体颜色
    context.font = "25px Arial"; //设置字体
    
    var rand = new Array();
    var x = new Array();
    var y = new Array();
    // Math.floor(Math.random()*(n-m+1)+m)//生成一个从 m - n 之间的随机整数
    for (var i = 0; i < 4; i++) {
      rand[i] = nums[Math.floor(Math.random() * nums.length)]
      x[i] = i * 20+10 ;//验证码坐标 横向距离 需要测试
      y[i] =Math.floor(Math.random()*(30-20+1)+20);
      context.fillText(rand[i], x[i], y[i]);
    }
    
    // console.log(rand,x,y)
    //画3条随机线
    for (var i = 0; i < 3; i++) {
      drawline(canvas, context);
    }

    // 画30个随机点
    for (var i = 0; i < 30; i++) {
      drawDot(canvas, context);
    }

    resolv({code:rand.join('')})
    // 随机线
    function drawline(canvas, context) {
      context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的起点x坐标是画布x坐标0位置，y坐标是画布高度的随机数
      context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height)); //随机线的终点x坐标是画布宽度，y坐标是画布高度的随机数
      context.lineWidth = 0.5; //随机线宽
      context.strokeStyle = 'rgba(50,50,50,0.3)'; //随机线描边属性
      context.stroke(); //描边，即起点描到终点
    }
    // 随机点(所谓画点其实就是画1px像素的线，方法不再赘述)
    function drawDot(canvas, context) {
      var px = Math.floor(Math.random() * canvas.width);
      var py = Math.floor(Math.random() * canvas.height);
      context.moveTo(px, py);
      context.lineTo(px + 1, py + 1);
      context.lineWidth = 0.2;
      context.stroke();
    }
    }catch(err){
      rejct(err)
    }

  })
}